package com.jsh.erp.utils;

import org.apache.commons.text.StrSubstitutor;
import org.apache.commons.text.StringSubstitutor;

import java.util.List;
import java.util.Map;

public class VectorStrUtil {
    //账目数据
    private static final String accountTemplateStr =
            "${type}类型财务单据信息:目标单位是${organ},经手人是${handsPerson},目标账户是${accountId},"
                    + "操作员是${creator},变动金额变动金额(优惠/收款/付款/实付)是${changeAmount},"
                    + "优惠金额是${discountMoney},合计金额是${totalPrice},"
                    + "单据编号是${billNo},单据日期是${billTime},备注：${remark}";
    private static final String inItemTemplateStrHead =
            "${type}单据，收入来源${organ},收款账户为${accountName},总金额为(${totalPrice})元,"
                    + "单号${billNo},日期${billTime}";

    private static final String inItemTemplateStrHead2 =
            "${type}单据，收入来源${organ},收款账户为${accountName},总金额为(${totalPrice})元,"
                    + "单号${billNo},日期${billTime}";

    private static final String outItemTemplateStrHead =
            "${type}单据，支出目标${organ},支出项目为#{},支出账户为${accountName},总金额为(${totalPrice})元,"
                    + "支出单号${billNo},日期${billTime}";

    private static final String outItemTemplateStrHead2 =
            "${type}单据，付款目标${organ},支出项目为#{},付款账户为${accountName},总金额为(${totalPrice})元,"
                    + "付款单号${billNo},日期${billTime}";

    private static final String transferTemplateStrHead =
            "${type}单据，转入账户${inAccount},转出账户为${outAccount}，金额为(${totalPrice1})元,"
                    + "经手人：${person}单号${billNo},日期${billTime},备注:${remark1}";



    //供应商/客户数据
    private static final String supplierTemplateStr =
            "${type}信息:名称为${supplier_name}，联系人是${contacts}，"
                    + "联系电话是${phone_num}，手机号码是${telephone},"
                    + "银行账号是${account_number}，开户行是${bank_name},纳税人识别号${tax_num}";

    //仓库
    private static final String depotTemplateStr =
            "仓库信息:仓库名为${depot_nema},地址位于${address}";

    //产品
    private static final String materialTemplateStr =
            "产品信息: 产品名称为${name},产品属于${category}类，型号为${model},规格为${standard}" +
                    "颜色(${color}),单位(${unit}),计量单位(${unit_id}),制造商(${mfrs}),备注:${remark}";

    private static final String materialCategoryTemplateStr =
            "产品类型信息: 产品类型为${name},创建时间为${createTime},上级类型(${partner}),备注:${remark}";

    private static final String materialCurrentStockTemplateStr =
            "库存信息: 存放产品为${name},存放数量(${number}),当前单价(${currentUnitPrice}元),存放仓库为${depot}";

    private static final String depotItemTemplateStrHead =
            "单据信息：类型为${actualType},单据号:${defaultNumber},当前状态为${status},详情:\n";

    private static final String depotItemSalesOrderTemplateStrHead =
            "单据信息：类型为${actualType},客户为${supplierName},单据号:${defaultNumber}," +
                    "订单总金额(${totalAmount}元),优惠率(${discount}%),优惠金额(${discountAmount}元)," +
                    "实际结算金额(${amount}元),当前状态为${status},详情:\n";

    private static final String otherdepotItemTemplateStrHead =
            "单据信息：类型为${actualType},单据号:${defaultNumber}";

    private static final String getDepotTemplateStrDetail =
            "商品名称:${materia},商品数量:${basicNumber},单位:${materiaUnit},对应仓库:${depotName}\n";

    private static final String depotTransferTemplateStrDetail =
            "商品名称:${materia},商品数量:${basicNumber},单位:${materiaUnit},调出仓库:${depotName}，调入仓库:${anotherDepotName}.\n";

    private static final String inOutItemTemplateStrHead = "收支项目名称为:${name},收支项目类型为:${type}";

    private static final String personTemplateStrDetail = "${type}：${name}";
    private static final String statusTemplateStrDetail = ",当前状态为:${status}";
    private static final String remarkStrDetail = ",备注:${remark}";


    public static String toVectorStr(Map<String, Object> map) {
        String remark = "";
        String remarkStr = (String) (map.get("remark"));
        if (remarkStr != null && remarkStr.length() > 0) {
            remark = StrSubstitutor.replace(remarkStrDetail, map);
        }
        switch ((String) (map.get("entryType"))) {
            //账目数据
            case "AccountHead":
                if ("收入".equals(map.get("type"))) {
                    return StrSubstitutor.replace(inItemTemplateStrHead, map);
                } else if ("支出".equals(map.get("type"))) {
                    return StrSubstitutor.replace(outItemTemplateStrHead, map);
                } else if ("收款".equals(map.get("type"))){
                    return StrSubstitutor.replace(inItemTemplateStrHead2, map);
                } else if ("付款".equals(map.get("type"))){
                    return StrSubstitutor.replace(outItemTemplateStrHead2, map);
                } else if ("转账".equals(map.get("type"))){
                    return StrSubstitutor.replace(transferTemplateStrHead, map);
                } else {
                    return StrSubstitutor.replace(accountTemplateStr, map);
                }
                //收支项目信息
            case "InOutItem":
                return StrSubstitutor.replace(inOutItemTemplateStrHead, map) + (remark == null ? "" : remark);
            //供应商/客户信息
            case "Supplier":
                return StrSubstitutor.replace(supplierTemplateStr, map);
            //仓库信息
            case "Depot":
                return StrSubstitutor.replace(depotTemplateStr, map);
            case "Person":
                return StrSubstitutor.replace(personTemplateStrDetail, map);
            //产品信息
            case "Material":
                return StrSubstitutor.replace(materialTemplateStr, map);
            case "MaterialCategory":
                return StrSubstitutor.replace(materialCategoryTemplateStr, map);
            case "MaterialCurrentStock":
                return StrSubstitutor.replace(materialCurrentStockTemplateStr, map);
            case "DepotItem":
                if ("其它入库".equals(map.get("actualType")) || "其它出库".equals(map.get("actualType"))) {
                    //其它单据
                    String s = StrSubstitutor.replace(otherdepotItemTemplateStrHead, map);
                    if ("其它入库".equals(map.get("actualType"))) {
                        s = s + "供应商为:" + map.get("supplierName") + ",";
                    } else if ("其它出库".equals(map.get("actualType"))) {
                        s = s + "客户为:" + map.get("supplierName") + ",";
                    }
                    s = s + StrSubstitutor.replace(statusTemplateStrDetail, map);
                    for (Map<String, Object> inMap : (List<Map<String, Object>>) map.get("inMaps")) {
                        s = s + StrSubstitutor.replace(getDepotTemplateStrDetail, inMap);
                    }
                    return s;
                }

                if ("调拨出库".equals(map.get("actualType"))) {
                    String s = StrSubstitutor.replace(otherdepotItemTemplateStrHead, map);
                    s = s + StringSubstitutor.replace(statusTemplateStrDetail, map) + ",详情：\n";
                    for (Map<String, Object> inMap : (List<Map<String, Object>>) map.get("inMaps")) {
                        s = s + StrSubstitutor.replace(depotTransferTemplateStrDetail, inMap);
                    }
                    return s;
                }
                String actualType = (String) (map.get("entryType"));
                switch (actualType) {
                    case "DepotItem":
                        //开头信息
                        if ("采购".equals(map.get("actualType"))) {
                            map.put("actualType", "采购入库");
                        }
                        String strHead;
                        if ("销售订单".equals(map.get("actualType"))
                                || "销售出库".equals(map.get("actualType"))
                        ) {
                            strHead = StrSubstitutor.replace(depotItemSalesOrderTemplateStrHead, map);
                            map.remove("supplierName");
                            map.remove("totalAmount");
                            map.remove("discount");
                            map.remove("discountAmount");
                            map.remove("amount");
                        } else {
                            strHead = StrSubstitutor.replace(depotItemTemplateStrHead, map);
                        }
                        StringBuilder sb = new StringBuilder(strHead);
                        String accountName = (String) (map.get("accountName"));
                        map.remove("entryType");
                        map.remove("actualType");
                        map.remove("status");
                        map.remove("accountName");
                        map.remove("defaultNumber");
                        //遍历子单据，补充详情信息
                        List<Map<String, Object>> inMaps = (List) map.get("inMaps");
                        for (Map<String, Object> inMap : inMaps) {
                            sb.append(StrSubstitutor.replace(getDepotTemplateStrDetail, inMap));
                        }
//                        for (String key : map.keySet()) {
//                            Map inMap = (Map)map.get(key);
//                            sb.append(StrSubstitutor.replace(getDepotTemplateStrDetail, inMap));
//                        }
                        String vectorStr = sb.toString();
                        if (accountName != null) {
                            vectorStr = vectorStr + "\n结算账户为：" + accountName;
                        }
                        return vectorStr;
                }
        }
        return null;
    }

}
